//
//  DetailOrderVC.m
//  LazyCat
//
//  Created by linhui on 15/12/15.
//  Copyright (c) 2015年 zhanshu. All rights reserved.
//

#import "DetailOrderVC.h"
#import <CoreBluetooth/CoreBluetooth.h>
#import "MSBluetoothManager.h"
#import "NJKWebViewProgress.h"
#import "NJKWebViewProgressView.h"
#import "CanceOrderlViewController.h"

@interface DetailOrderVC ()<CBCentralManagerDelegate,CBPeripheralDelegate,UIAlertViewDelegate,NJKWebViewProgressDelegate,UIWebViewDelegate,UITextFieldDelegate,backDelegate>
{
    UIView *_backView;//同意退货
    UIView *_backView2;//拒绝退货
    NSString *_money;//默认退货金额
    UITextField *_textField1;
    UITextField *_textField2;
    UIWebView *_detailWeb;//网页
    UIView *_backViewX;//最下方按钮背景
    NSString *_orderNum;
    NJKWebViewProgressView *_progressView;
    NJKWebViewProgress *_progressProxy;
    BOOL _finishLoad;//web页是否加载完毕
    
}
@property (nonatomic, retain) CBCentralManager *centralManager;
@property (nonatomic, retain) CBPeripheral *p;
@property (nonatomic, retain) CBCharacteristic * c;

@property (nonatomic, retain) NSString *UUID;


@property (nonatomic, retain) NSMutableArray *peripheralArray;
@property (nonatomic, retain) NSMutableArray *blueNameArr;

@end

@implementation DetailOrderVC

-(void)backStr:(NSString *)str {
    if ([str isEqualToString:@"TTT"]) {
        UIButton *yanShiFaHuo = (UIButton *)[self.view viewWithTag:9];//延时发货
        UIButton *btn = (UIButton *)[self.view viewWithTag:10];
        UIButton *btnFH = (UIButton *)[self.view viewWithTag:11];
        yanShiFaHuo.hidden = YES;
        btn.hidden = YES;
        btnFH.hidden = YES;
        [self loadWeb];//刷新状态
    }
//    else if ([str isEqualToString:@"TT"]){
//        
//    }
}
- (void)backAction:(UIButton *)btn {//判断是否可以HTML返回
    if ([self.tuisong isEqualToString:@"y"]) {
        [self dismissViewControllerAnimated:NO completion:^{
        }];
    }else{
        [self.navigationController popViewControllerAnimated:YES];
    }
}
- (void)touchesBegan
{
    NSString *RStr = @"2764";
    NSMutableString *str = [NSMutableString new];
    [str appendFormat:@"%c", 28];
    [str appendFormat:@"%c", 33];
    [str appendFormat:@"%c", 8];
    
    [[MSBluetoothManager shareInstance].cbPeripheral writeValue:[str dataUsingEncoding:CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000)] forCharacteristic:[MSBluetoothManager shareInstance].cbCharacteristcs type:CBCharacteristicWriteWithResponse];
    NSString *shopName = [ShopCommonModel share].shopname; // 商家名称
    NSString *orderno = [_dataArrray objectForKey:@"orderno"]; // 订单号
    NSString *createtime = [_dataArrray objectForKey:@"createtime"]; // 下单时间
    NSString *sendDay = [_dataArrray objectForKey:@"sendtime"]; // 配送日期
    NSString *sendtime = [_dataArrray objectForKey:@"sendtimerang"]; // 配送时间
    NSString *mobile = [_dataArrray objectForKey:@"mobile"]; // 联系电话
    NSString *address = [_dataArrray objectForKey:@"address"]; // 联系地址
    NSArray *temArr = [_dataArrray objectForKey:@"products"]; // 商品数组
    NSString *totalmoney = [_dataArrray objectForKey:@"totalmoney"]; // 总金额
    NSString *productsnum = [_dataArrray objectForKey:@"productsnum"]; // 商品数量
    NSString *paytypestr = [_dataArrray objectForKey:@"paytypestr"]; // 支付方式
    NSString *onlinePay = [_dataArrray objectForKey:@"realpayamount"];
    NSString *yuePay = [_dataArrray objectForKey:@"yue"];
    NSString *jifenPay = [_dataArrray objectForKey:@"fenxiangjf"];
    NSString *yhqPay = [_dataArrray objectForKey:@"couponpay"];
    
    NSString *product = @"";
    for (int i = 0; i < temArr.count; i++) {
        NSString *pName = [temArr[i] objectForKey:@"productname"]; // 商品名字
        NSString *price = [temArr[i] objectForKey:@"price"]; // 商品价格
        NSString *numbuy = [temArr[i] objectForKey:@"numbuy"]; // 商品价格
        product = [NSString stringWithFormat:@"%@\n%@\n                x%@       %@\n",product,pName,numbuy,price];
        
    }
    
    //    RStr = @"杜博 你好呀!!!\n我很好\n啦啦啦\n啦啦啦\n啦啦\n啦啦啦\n啦啦啦\n啦啦\n";
    RStr = [NSString stringWithFormat:@"\n商家名称:%@\n订单号:%@\n下单时间:%@\n配送日期:%@\n配送时间:%@\n联系电话:%@\n联系地址:%@\n------------------------------\n商品名称       数量      金额\n------------------------------\n%@\n------------------------------\n商品总数量:%@\n支付方式:%@\n在线支付:%@\n余额支付:%@\n积分抵扣:%@\n优惠券抵扣:%@\n消费总金额:%@\n------------------------------\n 懒猫社长\n\n\n",shopName,orderno,createtime,sendDay,sendtime,mobile,address,product,productsnum,paytypestr,onlinePay,yuePay,jifenPay,yhqPay,totalmoney];
    
    NSData *nsdata =  [RStr dataUsingEncoding:CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000)];
    [[MSBluetoothManager shareInstance] writeData:nsdata];
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self createUI];
    [self loadWeb];
    
}
-(void)hideKeyBoard:(UITapGestureRecognizer *)tap {
    [_textField1 resignFirstResponder];
    [_textField2 resignFirstResponder];
    [self.view resignFirstResponder];
}
- (void)loadWeb {
    NSString *url;
    if (_tuiHuoManager) {
        url = [NSString stringWithFormat:detailTuiHuoUrl,_orderNum,[AppData share].username,[Utils md5SignStr]];
    }else {
        //url = [detailOrderUrl stringByAppendingString:_orderNum];
        url = [NSString stringWithFormat:detailOrderUrl,_orderNum,[AppData share].username,[Utils md5SignStr]];
        NSLog(@"%@",url);
    }
    NSUserDefaults *userDefaul = [NSUserDefaults standardUserDefaults];
    NSString *iptype = [userDefaul objectForKey:@"iptype"];
    NSString *temUrl = [NSString stringWithFormat:@"%@&terminal=%@",url,iptype];
    // 判断是否签名
    if([temUrl rangeOfString:@"sign2"].location !=NSNotFound)//_roaldSearchText
    {
        // 存在
    }else{
        // 不存在
        temUrl = [NSString stringWithFormat:@"%@&sign2=%@",temUrl,[Utils md5SignStr]];
    }
    NSString *lower = [temUrl lowercaseString];
    if ([lower rangeOfString:@"username"].location !=NSNotFound) {
        // 存在
    }else{
        // 不存在
        temUrl = [NSString stringWithFormat:@"%@&Username=%@",temUrl,[AppData share].username];
    }
    [_detailWeb loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:temUrl]]];
}
//#pragma mark - NJKWebViewProgressDelegate
//-(void)webViewProgress:(NJKWebViewProgress *)webViewProgress updateProgress:(float)progress
//{
//    [_progressView setProgress:progress animated:YES];
//    NSLog(@"--------------------------%f",progress);
//}
- (void)createUI {
    _finishLoad = NO;
    self.view.backgroundColor = BackColor;
    [self initNav:@"订单详情" doBack:YES];
    UIView *lineV = [[UIView alloc]initWithFrame:CGRectMake(0, 63, ScreenWidth, 1)];
    lineV.backgroundColor = [UIColor lightGrayColor];
    lineV.alpha = 0.2;
    [self.view addSubview:lineV];
    if (_tuiHuoManager) {
        _detailWeb = [[UIWebView alloc] initWithFrame:CGRectMake(0, 64,ScreenWidth,ScreenHeight - 128)];
    }else {
        _detailWeb = [[UIWebView alloc] initWithFrame:CGRectMake(0, 64,ScreenWidth,ScreenHeight - 128)];
    }
    _detailWeb.backgroundColor = BackColor;
    [self.view addSubview:_detailWeb];
    //webview自带进度条。。
    _detailWeb.scrollView.showsVerticalScrollIndicator = NO;
#pragma mark -- 进度条1
    _progressProxy = [[NJKWebViewProgress alloc]init];
    _detailWeb.delegate = _progressProxy;
    _progressProxy.webViewProxyDelegate = self;
    _progressProxy.progressDelegate = self;
    CGRect navBounds = self.navBar.bounds;
    CGRect barFrame = CGRectMake(0, navBounds.size.height - 2, 0, 2);
    _progressView = [[NJKWebViewProgressView alloc] initWithFrame:barFrame];
    _progressView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;
    [UIView animateWithDuration:1 animations:^{
        _progressView.frame = CGRectMake(_progressView.frame.origin.x, _progressView.frame.origin.y, ScreenWidth - ScreenWidth/2+80, 2);
    } completion:^(BOOL finished) {
//        if (_finishLoad) {
//            _progressView.hidden = YES;
//        }
    }];
    
    
    //底部背景
    _backViewX = [[UIView alloc]initWithFrame:CGRectMake(0, ScreenHeight - 64, ScreenWidth, 64)];
    [self.view addSubview:_backViewX];
    _backViewX.backgroundColor = [UIColor whiteColor];
    UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 1)];
    lineView.backgroundColor = [UIColor lightGrayColor];
    lineView.alpha = 0.2;
    [_backViewX addSubview:lineView];
    if (_tuiHuoManager) {
        UIButton *button1 = [UIButton buttonWithType:UIButtonTypeSystem];
        button1.frame = CGRectMake((ScreenWidth - 120)/2, 15, 120, 34);
        button1.tag = 99;
        [button1 setTitle:@"退货处理中" forState:UIControlStateNormal];
        [button1 setTitleColor:SetColor(102, 102, 102, 1) forState:UIControlStateNormal];
        button1.layer.borderColor = SetColor(0, 0, 0, 0.2).CGColor;
        button1.layer.borderWidth = 1;
        button1.layer.cornerRadius = 3;
        button1.clipsToBounds = YES;
        
        [button1 addTarget:self action:@selector(fourBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        button1.backgroundColor = [UIColor whiteColor];
        [_backViewX addSubview:button1];
        NSArray *tuiHuoArr = @[@"同意退货",@"拒绝退货"];
        for (int i = 0; i<tuiHuoArr.count; i++) {
            UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
            button.frame = CGRectMake(20 + i*((ScreenWidth - 20*3)/2+20), 15, (ScreenWidth - 20*3)/2, 34);
            button.tag = 100+i;
            //button.hidden = YES;
            [button setTitle:tuiHuoArr[i] forState:UIControlStateNormal];
            [button setTitleColor:SetColor(102, 102, 102, 1) forState:UIControlStateNormal];
            button.layer.borderColor = SetColor(0, 0, 0, 0.2).CGColor;
            button.layer.borderWidth = 1;
            button.layer.cornerRadius = 3;
            button.clipsToBounds = YES;
            [button addTarget:self action:@selector(fourBtnClick:) forControlEvents:UIControlEventTouchUpInside];
            button.backgroundColor = [UIColor whiteColor];
            [_backViewX addSubview:button];
            
        }
        //判断退货完成是否显示
        //建造最后一个button
        UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
        button.frame = CGRectMake((ScreenWidth - (ScreenWidth - 20*3)/2)/2, 15, (ScreenWidth - 20*3)/2, 34);
        button.tag = 800;
        [button setTitle:@"退货完成" forState:UIControlStateNormal];
        [button setTitleColor:SetColor(102, 102, 102, 1) forState:UIControlStateNormal];
        button.layer.borderColor = SetColor(0, 0, 0, 0.2).CGColor;
        button.layer.borderWidth = 1;
        button.layer.cornerRadius = 3;
        button.clipsToBounds = YES;
        [button addTarget:self action:@selector(fourBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        button.backgroundColor = [UIColor whiteColor];
        [_backViewX addSubview:button];
        if ([_wanCheng isEqualToString:@"1"]) {
            button.hidden = NO;
        }else {
            button.hidden = YES;
        }
        //判断退货按钮显示
        UIButton *btn1 = (UIButton *)[self.view viewWithTag:100];//同意
        UIButton *btn2 = (UIButton *)[self.view viewWithTag:101];
        if ([_clzBtn isEqualToString:@"1"]) {//处理中显示
            button1.hidden = NO;
            btn1.hidden = YES;
            btn2.hidden = YES;
        }else {//处理中不显示
            button1.hidden = YES;
            if ([_juJue isEqualToString:@"0"]) {
                btn2.hidden = YES;
            }
            if ([_tongYi isEqualToString:@"0"]) {
                btn1.hidden = YES;
            }
            if ([_juJue isEqualToString:@"0"]&&[_tongYi isEqualToString:@"0"]&&[_clzBtn isEqualToString:@"0"]&&[_wanCheng isEqualToString:@"0"]) {
                _backViewX.hidden = YES;
                lineView.hidden = YES;
                _detailWeb.frame = CGRectMake(0, 64,ScreenWidth,ScreenHeight - 64);
            }
        }
        NSString *sumUrl = [NSString stringWithFormat:refundAmountUrl,_orderNum];
        [NTNetUtil sendRequestWithUrl:sumUrl requestName:nil method:NTRequestMethodGET parameters:nil block:^(NTHttpRequest *networkRequest, NSError *error) {
            NSDictionary *dic = networkRequest.responseData;
            NSString *result = [dic objectForKey:@"m"];
            if ([result isEqualToString:@"success"]) {
                _money = [dic objectForKey:@"maxrefundamount"];
                [AppData share].isLoadd = YES;
            }else{
                
            }
        }];
    }else {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
        button.frame = CGRectMake(ScreenWidth - 75, 25, 65, 34);
        button.tag = 13;
        button.titleLabel.font = [UIFont systemFontOfSize:13];
        [button setTitle:@"联系买家" forState:UIControlStateNormal];
        [button setTitleColor:SetColor(102, 102, 102, 1) forState:UIControlStateNormal];
        [button addTarget:self action:@selector(fourBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        button.backgroundColor = [UIColor whiteColor];
        [self.navBar addSubview:button];
        NSArray *titleArr = @[@"延时发货",@"取消订单",@"订单发货",@"打印小票"];
        for (int i = 0; i<titleArr.count; i++) {
            UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
            button.frame = CGRectMake(10 + i*((ScreenWidth - 10*5)/4+10), 15, (ScreenWidth - 10*5)/4, 34);
            button.tag = 9+i;
            button.titleLabel.font = [UIFont systemFontOfSize:13];
            [button setTitle:titleArr[i] forState:UIControlStateNormal];
            //button.titleLabel.font = [UIFont systemFontOfSize:12];
            //[button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            [button setTitleColor:SetColor(102, 102, 102, 1) forState:UIControlStateNormal];
            button.layer.borderColor = SetColor(0, 0, 0, 0.2).CGColor;
            button.layer.borderWidth = 1;
            button.layer.cornerRadius = 3;
            button.clipsToBounds = YES;
            [button addTarget:self action:@selector(fourBtnClick:) forControlEvents:UIControlEventTouchUpInside];
            button.backgroundColor = [UIColor whiteColor];
            [_backViewX addSubview:button];
        }
    }
    //判段三个按钮显隐
    UIButton *yanshi = (UIButton *)[self.view viewWithTag:9];
    UIButton *quxiao = (UIButton *)[self.view viewWithTag:10];
    UIButton *fahuo = (UIButton *)[self.view viewWithTag:11];
    if ([_fahuo isEqualToString:@"0"]) {
        fahuo.hidden = YES;
    }
    if ([_quxiao isEqualToString:@"0"]) {
        quxiao.hidden = YES;
    }
    if ([_yanshi isEqualToString:@"0"]) {
        yanshi.hidden = YES;
    }
    if ([_fahuo isEqualToString:@"1"]) {//发货显示
        fahuo.hidden = NO;
        if ([_quxiao isEqualToString:@"1"]) {//取消显示
            if ([_yanshi isEqualToString:@"1"]) {//延时显示
                
            }else {
                yanshi.hidden = YES;
            }
        }else {
            quxiao.hidden = YES;
            yanshi.frame = quxiao.frame;
        }
    }else {//发货隐藏
        fahuo.hidden = YES;
        if ([_quxiao isEqualToString:@"1"]) {//取消显示
            quxiao.hidden = NO;
            yanshi.frame = quxiao.frame;
            quxiao.frame = fahuo.frame;
        }else {//取消隐藏
            quxiao.hidden = YES;
            yanshi.frame = fahuo.frame;
        }
    }
}
- (void)fourBtnClick:(UIButton *)sender {
    if (sender.tag == 9) {//延时发货
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:nil message:@"延时发货" delegate:self cancelButtonTitle:@"否" otherButtonTitles:@"是", nil];
        alert.tag = 1991;
        [alert show];
    }else
        if (sender.tag == 10) {//取消订单
            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:nil message:@"确认取消" delegate:self cancelButtonTitle:@"否" otherButtonTitles:@"是", nil];
            alert.tag = 1992;
            [alert show];
        }else if (sender.tag == 11) {//订单发货
            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:nil message:@"确认发货" delegate:self cancelButtonTitle:@"否" otherButtonTitles:@"是", nil];
            alert.tag = 1993;
            [alert show];
        }else if (sender.tag == 12) {//打印小票
            [self touchesBegan];
        }else if (sender.tag == 13) {//联系买家
            NSString *mobile = [_dataArrray objectForKey:@"mobile"];
            NSString *user = [_dataArrray objectForKey:@"linkman"];
            if (user == nil) {
                user = @"";
            }
            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:nil message:[NSString stringWithFormat:@"买家姓名:%@\n联系电话：%@\n是否通话？",user,mobile] delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
            alert.tag = 1994;
            [alert show];
        }else if (sender.tag == 100){//同意退货
            _backView = [[UIView alloc]initWithFrame:CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y, self.view.frame.size.width, self.view.frame.size.height)];
            _backView.backgroundColor = SetColor(1, 1, 1, 0.2);
            //蒙版隐藏键盘
            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideKeyB)];
            [_backView addGestureRecognizer:tap];
            [self.view addSubview:_backView];
            UIView *bcView = [[UIView alloc]initWithFrame:CGRectMake((ScreenWidth - 280)/2, self.view.frame.size.height/2-60, 280, 120)];
            bcView.backgroundColor = [UIColor whiteColor];
            bcView.layer.cornerRadius = 3;
            bcView.clipsToBounds = YES;
            [_backView addSubview:bcView];
            UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 280, 39)];
            label.text = @"同意退货";
            label.textAlignment = NSTextAlignmentCenter;
            label.font = [UIFont systemFontOfSize:17];
            [bcView addSubview:label];
            UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(0, 39, ScreenWidth, 1)];
            lineView.backgroundColor = [UIColor lightGrayColor];
            lineView.alpha = 0.2;
            [bcView addSubview:lineView];
            bcView.tag = 66;
            UILabel *leftLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 40, 70, 40)];
            leftLabel.text = @"退款金额";
            leftLabel.font = [UIFont systemFontOfSize:14];
            [bcView addSubview:leftLabel];
            _textField1 = [[UITextField alloc]initWithFrame:CGRectMake(80, 48, 190, 24)];
            _textField1.borderStyle = UITextBorderStyleRoundedRect;
            [bcView addSubview:_textField1];
            _textField1.tag = 88;
            _textField1.delegate = self;
            _textField1.text = _money;
            _textField1.font = [UIFont systemFontOfSize:14];
            UIView *lineView2 = [[UIView alloc]initWithFrame:CGRectMake(0, 79, ScreenWidth, 1)];
            lineView2.backgroundColor = [UIColor lightGrayColor];
            lineView2.alpha = 0.2;
            [bcView addSubview:lineView2];
            NSArray *btnArr = @[@"取消",@"确定"];
            for (int i = 0;i<btnArr.count ; i++) {
                UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
                btn1.frame = CGRectMake(i*(280/2), 80, 140, 40);
                btn1.tag = 20+i;
                [btn1 setTitle:btnArr[i] forState:UIControlStateNormal];
                [btn1 setTitleColor:MainColor forState:UIControlStateNormal];
                btn1.layer.borderWidth = 0.2;
                btn1.layer.borderColor = SetColor(0, 0, 0, 0.2).CGColor;
                [btn1 addTarget:self action:@selector(bcViewBtn:) forControlEvents:UIControlEventTouchUpInside];
                [bcView addSubview:btn1];
                btn1.titleLabel.font = [UIFont systemFontOfSize:15];
            }
            
        }else if (sender.tag == 101){//拒绝退款
            _backView2 = [[UIView alloc]initWithFrame:CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y, self.view.frame.size.width, self.view.frame.size.height)];
            _backView2.backgroundColor = SetColor(1, 1, 1, 0.2);
            [self.view addSubview:_backView2];
            
            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(hideKeyBoard:)];
            tap.cancelsTouchesInView = NO;
            [_backView2 addGestureRecognizer:tap];
            
            UIView *bcView = [[UIView alloc]initWithFrame:CGRectMake((ScreenWidth - 280)/2, self.view.frame.size.height/2-60, 280, 120)];
            bcView.backgroundColor = [UIColor whiteColor];
            bcView.layer.cornerRadius = 3;
            bcView.clipsToBounds = YES;
            bcView.tag = 77;
            [_backView2 addSubview:bcView];
            UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 280, 39)];
            label.text = @"拒绝退货";
            label.textAlignment = NSTextAlignmentCenter;
            label.font = [UIFont systemFontOfSize:17];
            [bcView addSubview:label];
            UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(0, 39, ScreenWidth, 1)];
            lineView.backgroundColor = [UIColor lightGrayColor];
            lineView.alpha = 0.2;
            [bcView addSubview:lineView];
            UILabel *leftLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 40, 70, 40)];
            leftLabel.text = @"拒绝原因";
            leftLabel.font = [UIFont systemFontOfSize:14];
            [bcView addSubview:leftLabel];
            _textField2 = [[UITextField alloc]initWithFrame:CGRectMake(80, 48, 190, 24)];
            _textField2.borderStyle = UITextBorderStyleRoundedRect;
            _textField2.tag = 89;
            [bcView addSubview:_textField2];
            _textField2.font = [UIFont systemFontOfSize:14];
            UIView *lineView2 = [[UIView alloc]initWithFrame:CGRectMake(0, 79, ScreenWidth, 1)];
            lineView2.backgroundColor = [UIColor lightGrayColor];
            lineView2.alpha = 0.2;
            [bcView addSubview:lineView2];
            NSArray *btnArr = @[@"取消",@"确定"];
            for (int i = 0;i<btnArr.count ; i++) {
                UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
                btn1.frame = CGRectMake(i*(280/2), 80, 140, 40);
                btn1.tag = 30+i;
                [btn1 setTitle:btnArr[i] forState:UIControlStateNormal];
                [btn1 setTitleColor:MainColor forState:UIControlStateNormal];
                btn1.layer.borderWidth = 0.2;
                btn1.layer.borderColor = SetColor(0, 0, 0, 0.2).CGColor;
                [btn1 addTarget:self action:@selector(bcViewBtn:) forControlEvents:UIControlEventTouchUpInside];
                [bcView addSubview:btn1];
                btn1.titleLabel.font = [UIFont systemFontOfSize:15];
            }
        }else if (sender.tag == 99){//退货处理中
            UIButton *btn1 = (UIButton *)[self.view viewWithTag:100];
            UIButton *btn2 = (UIButton *)[self.view viewWithTag:101];
            NSString *url = [NSString stringWithFormat:tuiHuoBtnUrl,@"0",[AppData share].username,@"",_orderNum,@""];
            [NTNetUtil sendRequestWithUrl:url requestName:nil method:NTRequestMethodGET parameters:nil block:^(NTHttpRequest *networkRequest, NSError *error) {
                NSDictionary *dic = networkRequest.responseData;
                NSString *result = [dic objectForKey:@"m"];
                if ([result isEqualToString:@"success"]) {
                    [[MessShowView share]showWithMess:@"操作成功" login:YES];
                    sender.hidden = YES;
                    btn1.hidden = NO;
                    btn2.hidden = NO;
                    [AppData share].isLoadd = YES;
                }else{
                    //[[MessShowView share]showWithMess:@"退款失败"];
                    [[MessShowView share]showWithMess:@"操作失败" login:NO];
                }
            }];
        }else if (sender.tag == 800){//退货完成
//            NSString *money;
//            if (_textField1.text ==nil) {
//                money = @"";
//            }else {
//                money = _textField1.text;
//            }
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"退货完成" delegate:self cancelButtonTitle:@"否" otherButtonTitles:@"是", nil];
            alert.tag = 1234;
            [alert show];
        }
}
//同意退货是否按钮
- (void)bcViewBtn:(UIButton *)sender {
    UIButton *TY = (UIButton *)[self.view viewWithTag:100];
    UIButton *JJ = (UIButton *)[self.view viewWithTag:101];
    UIButton *wanCheng = (UIButton *)[self.view viewWithTag:800];
    if (sender.tag == 20) {//同意：取消
        NSLog(@"20");
        _backView.hidden = YES;
        [_textField1 resignFirstResponder];
        [_textField2 resignFirstResponder];
    }else if (sender.tag == 21){//同意：确定
        if (_textField1.text.length == 0) {
            [[MessShowView share]showWithMess:@"退款金额不能为空"];
            return;
        }else {
            _backView.hidden = YES;
            NSString *url = [NSString stringWithFormat:tuiHuoBtnUrl,@"2",[AppData share].username,_textField1.text,_orderNum,@""];
            [NTNetUtil sendRequestWithUrl:url requestName:nil method:NTRequestMethodGET parameters:nil block:^(NTHttpRequest *networkRequest, NSError *error) {
                NSDictionary *dic = networkRequest.responseData;
                NSString *result = [dic objectForKey:@"m"];
                if ([result isEqualToString:@"success"]) {
                    //[[MessShowView share]showWithMess:@"退款成功"];
                    [[MessShowView share]showWithMess:@"操作成功" login:YES];
                    //刷新，去button
                    //[self createUI];
                    //                    _backViewX.hidden = YES;
                    //                    _detailWeb.frame = CGRectMake(0, 64,ScreenWidth,ScreenHeight - 64);
                    TY.hidden = YES;
                    JJ.hidden = YES;
                    wanCheng.hidden = NO;
                    [AppData share].isLoadd = YES;
                    [self loadWeb];
                }else{
                    [[MessShowView share]showWithMess:@"操作失败" login:NO];
                }
            }];
            [_textField1 resignFirstResponder];
            [_textField2 resignFirstResponder];
        }
    }else if (sender.tag == 30){//拒绝：qx
        _backView2.hidden = YES;
        [_textField1 resignFirstResponder];
        [_textField2 resignFirstResponder];
    }else if (sender.tag == 31){//拒绝：qd
        if (_textField2.text.length == 0) {
            [[MessShowView share]showWithMess:@"请填写拒绝原因"];
            return;
        }else {
            _backView2.hidden = YES;
            NSString *url = [NSString stringWithFormat:tuiHuoBtnUrl,@"1",[AppData share].username,@"",_orderNum,_textField2.text];
            [NTNetUtil sendRequestWithUrl:url requestName:nil method:NTRequestMethodGET parameters:nil block:^(NTHttpRequest *networkRequest, NSError *error) {
                NSDictionary *dic = networkRequest.responseData;
                NSString *result = [dic objectForKey:@"m"];
                if ([result isEqualToString:@"success"]) {
                    //[[MessShowView share]showWithMess:@"成功"];
                    [[MessShowView share]showWithMess:@"操作成功" login:YES];
                    _backView.hidden = YES;
                    [self createUI];
                    _backViewX.hidden = YES;
                    _detailWeb.frame = CGRectMake(0, 64,ScreenWidth,ScreenHeight - 64);
                    [AppData share].isLoadd = YES;
                    [self loadWeb];
                }else{
                    //[[MessShowView share]showWithMess:@"失败"];
                    [[MessShowView share]showWithMess:@"操作失败" login:NO];
                }
            }];
            [_textField1 resignFirstResponder];
            [_textField2 resignFirstResponder];
        }
    }
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (alertView.tag == 1991) {//延时发货
        if (buttonIndex == 1) {
            CanceOrderlViewController *cancel = [[CanceOrderlViewController alloc] init];
            cancel.orderType = @"1";
            cancel.delegate = self;
            cancel.orderNumber = _orderNum;
            if ([self.tuisong isEqualToString:@"y"]) {
                cancel.tuisong = self.tuisong;
                [self presentViewController:cancel animated:NO completion:^{
                }];
            }else{
                [self.navigationController pushViewController:cancel animated:YES];
            }
            [AppData share].isLoadd = YES;
        }
    }
    if (alertView.tag == 1992) {//取消订单
        if (buttonIndex == 1){
            UIButton *btn = (UIButton *)[self.view viewWithTag:10];
            UIButton *btnFH = (UIButton *)[self.view viewWithTag:11];
            //NSString *url = [NSString stringWithFormat:quxiaoOrder,self.orderNum,[AppData share].username];
            CanceOrderlViewController *cancel = [[CanceOrderlViewController alloc]init];
            cancel.orderType = @"0";
            cancel.delegate = self;
            cancel.orderNumber = _orderNum;
            if ([self.tuisong isEqualToString:@"y"]) {
                cancel.tuisong = self.tuisong;
                [self presentViewController:cancel animated:NO completion:^{
                }];
            }else{
                [self.navigationController pushViewController:cancel animated:YES];
            }
            [AppData share].isLoadd = YES;
        }
    }else if (alertView.tag == 1993){//订单发货
        if (buttonIndex == 1){
            UIButton *btn = (UIButton *)[self.view viewWithTag:11];
            NSUserDefaults *userDefaul = [NSUserDefaults standardUserDefaults];
            NSString *cid = [userDefaul objectForKey:@"cid"];
            NSString *url = [NSString stringWithFormat:fahuoOrder,_orderNum,[AppData share].username,cid];
            [NTNetUtil sendRequestWithUrl:url requestName:nil method:NTRequestMethodGET parameters:nil block:^(NTHttpRequest *networkRequest, NSError *error) {
                NSDictionary *dic = networkRequest.responseData;
                NSString *result = [dic objectForKey:@"m"];
                
                if ([result isEqualToString:@"success"]) {
                    [[MessShowView share]showWithMess:@"订单发货成功" login:YES];
                    btn.hidden = YES;
                    UIButton *yanshi = (UIButton *)[self.view viewWithTag:9];
                    UIButton *quxiao = (UIButton *)[self.view viewWithTag:10];
                    btn.enabled = NO;
                    yanshi.frame = quxiao.frame;
                    quxiao.frame = btn.frame;
                    yanshi.hidden = YES;
                    [self loadWeb];//刷新状态
                    [AppData share].isLoadd = YES;
                }else {
                    [[MessShowView share]showWithMess:@"订单发货失败" login:NO];
                }
                
            }];
        }
    }else if (alertView.tag == 1994){//联系买家
        if (buttonIndex == 1) {
            NSString *mobile = [_dataArrray objectForKey:@"mobile"];
            NSMutableString * str1=[[NSMutableString alloc] initWithFormat:@"tel://%@",mobile];
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str1]];
        }else {
            return;
        }
    }else if (alertView.tag == 1234){
        if (buttonIndex == 1) {
        NSString *url = [NSString stringWithFormat:tuiHuoBtnUrl,@"3",[AppData share].username,@"",_orderNum,@""];
        [NTNetUtil sendRequestWithUrl:url requestName:nil method:NTRequestMethodGET parameters:nil block:^(NTHttpRequest *networkRequest, NSError *error) {
            NSDictionary *dic = networkRequest.responseData;
            NSString *result = [dic objectForKey:@"m"];
            if ([result isEqualToString:@"success"]) {
                [[MessShowView share]showWithMess:@"操作成功" login:YES];
                //刷新，去button
                [self createUI];
                _backViewX.hidden = YES;
                _detailWeb.frame = CGRectMake(0, 64,ScreenWidth,ScreenHeight - 64);
                [self loadWeb];
                [AppData share].isLoadd = YES;
            }else{
                [[MessShowView share]showWithMess:@"操作失败" login:NO];
            }
        }];
        }
    }
}
//隐藏键盘
- (void)hideKeyB {
    UIView *bcView1 = (UIView *)[self.view viewWithTag:66];
    UIView *bcView2 = (UIView *)[self.view viewWithTag:77];
    [bcView1 resignFirstResponder];
    [bcView2 resignFirstResponder];
    [_textField1 resignFirstResponder];
    [_textField2 resignFirstResponder];
}
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    //[AppData share].isLoadd = YES;
    [_progressView removeFromSuperview];
}
//限制输入内容格式
- (BOOL) textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    //if (textField == _priceField) {
    NSScanner      *scanner    = [NSScanner scannerWithString:string];
    NSCharacterSet *numbers;
    NSRange         pointRange = [textField.text rangeOfString:@"."];
    
    if ( (pointRange.length > 0) && (pointRange.location < range.location  || pointRange.location > range.location + range.length) )
    {
        numbers = [NSCharacterSet characterSetWithCharactersInString:@"0123456789"];
    }
    else
    {
        numbers = [NSCharacterSet characterSetWithCharactersInString:@"0123456789."];
    }
    
    if ( [textField.text isEqualToString:@""] && [string isEqualToString:@"."] )
    {
        return NO;
    }
    
    short remain = 2; //默认保留2位小数
    
    NSString *tempStr = [textField.text stringByAppendingString:string];
    NSUInteger strlen = [tempStr length];
    if(pointRange.length > 0 && pointRange.location > 0){ //判断输入框内是否含有“.”。
        if([string isEqualToString:@"."]){ //当输入框内已经含有“.”时，如果再输入“.”则被视为无效。
            return NO;
        }
        if(strlen > 0 && (strlen - pointRange.location) > remain+1){ //当输入框内已经含有“.”，当字符串长度减去小数点前面的字符串长度大于需要要保留的小数点位数，则视当次输入无效。
            return NO;
        }
    }
    
    NSRange zeroRange = [textField.text rangeOfString:@"0"];
    if(zeroRange.length == 1 && zeroRange.location == 0){ //判断输入框第一个字符是否为“0”
        if(![string isEqualToString:@"0"] && ![string isEqualToString:@"."] && [textField.text length] == 1){ //当输入框只有一个字符并且字符为“0”时，再输入不为“0”或者“.”的字符时，则将此输入替换输入框的这唯一字符。
            textField.text = string;
            return NO;
        }else{
            if(pointRange.length == 0 && pointRange.location > 0){ //当输入框第一个字符为“0”时，并且没有“.”字符时，如果当此输入的字符为“0”，则视当此输入无效。
                if([string isEqualToString:@"0"]){
                    return NO;
                }
            }
        }
    }
    
    NSString *buffer;
    if ( ![scanner scanCharactersFromSet:numbers intoString:&buffer] && ([string length] != 0) )
    {
        return NO;
    }
    //}
    return YES;
}
#pragma mark -- 进度条2
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.navBar addSubview:_progressView];
}
//加载完成
- (void)webViewDidFinishLoad:(UIWebView *)webView {
    [UIView animateWithDuration:1 animations:^{
        _progressView.frame = CGRectMake(_progressView.frame.origin.x, _progressView.frame.origin.y, ScreenWidth, 2);
    } completion:^(BOOL finished) {
        _finishLoad = YES;
        _progressView.hidden = YES;
    }];
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
    _progressView.hidden = YES;
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [_textField1 resignFirstResponder];
    [_textField2 resignFirstResponder];
    [self.view resignFirstResponder];
}
@end
